﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Drawing.Text;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECare.BUS;
using ECare.BUS.Data;
using ECare.Client.frms;

namespace ECare.Win.controls
{
    public partial class ucStorage : BaseModule
    {
        public ucStorage()
        {
            InitializeComponent();

            this.Load += ucStorage_Load;

            this.grd.CellFormatting += grd_CellFormatting;
            this.grd.DataError += grd_DataError;
            this.grd.CellFormatting += grd_CellFormatting;
            this.windowsUIButtonPanel1.ButtonClick += windowsUIButtonPanel1_ButtonClick;


            this.grd.CellDoubleClick += grd_CellDoubleClick;
        }

        void grd_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //throw new NotImplementedException();
            if (e.RowIndex == -1)
                return;

            var entitye = this.grd.Rows[e.RowIndex].DataBoundItem as SupplyInfo;
            var frm = new frmInInfoReadonly();
            frm.CurrentSupply = entitye;
            frm.ShowDialog();
        }

        void windowsUIButtonPanel1_ButtonClick(object sender, DevExpress.XtraBars.Docking2010.ButtonEventArgs e)
        {
            //throw new NotImplementedException();

            if (e.Button.Properties.Caption == "New")
            {
                var frm = new frmInInfo();
                frm.entity = null;
                frm.Saved += frm_Saved;
                frm.ShowDialog();
            }
            else if (e.Button.Properties.Caption == "Edit")
            {
                if (this.grd.SelectedRows.Count == 0)
                    return;
                var entitye = this.grd.SelectedRows[0].DataBoundItem as SupplyInfo;


                if (entitye.supply_create_by != ApplicationData.CurrentUserInfo.UserId)
                {
                    MessageBox.Show("您只能修改自己的入库单据.");
                    return;}



                //如果已经质检完成或者特殊采用了，就不能在被修改了.
                if (entitye.supply_checkstatus == CheckStatus.Qualitified || entitye.supply_checkstatus==CheckStatus.SpecialAcceptance)
                {
                    MessageBox.Show("当前订单已经被质检完成,不能被修改!");
                    return;
                }
                var frm = new frmInInfo();
                frm.entity = entitye;
                frm.Saved += frm_Saved;
                frm.ShowDialog();
            }
            else if (e.Button.Properties.Caption == "Query")
            {
                var frm = new frmSupplyQuery();
                frm.SearchCompleted += frm_SearchCompleted;
                frm.ShowDialog();
            }
            else if (e.Button.Properties.Caption == "Status")
            {
                if (this.grd.SelectedRows.Count == 0)
                    return;
                var entitye = this.grd.SelectedRows[0].DataBoundItem as SupplyInfo;

                var frm = new frmCheckRecords();
                frm.checktype = 0;
                frm.entityId = entitye.supply_id;
                frm.ShowDialog();
            }
            else if (e.Button.Properties.Caption == "In")
            {
                if (this.grd.SelectedRows.Count == 0)
                    return;
                var entitye = this.grd.SelectedRows[0].DataBoundItem as SupplyInfo;
                if (entitye.HasStorage){
                    MessageBox.Show("已经入原材料库.");
                    return;
                }

                if (entitye.supply_checkstatus != CheckStatus.Qualitified  && entitye.supply_checkstatus!=CheckStatus.SpecialAcceptance)
                {
                    MessageBox.Show("质检未完成.", "信息提示");
                    return;
                }

                //只有质检合格以及特殊采用的，方可继续使用。//if (MessageBox.Show("确认入原材料库?", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) ==
                //    DialogResult.No)//    return;
                var frm = new frmCreateStorage();
                frm.entity = entitye;
                frm.ShowDialog();
            }
            else if (e.Button.Properties.Caption == "Cancel")
            {
                //
                this.CancelInStorage();
            }

            

        }

        private void CancelInStorage(){
            if (this.grd.SelectedRows.Count == 0)
                return;
            var entitye = this.grd.SelectedRows[0].DataBoundItem as SupplyInfo;
            //取消入库
            var frm = new frmInInfoReadonly();
            frm.IsCancel = true;
            frm.Deactived += frm_Deactived;
            frm.entity = entitye;
            frm.ShowDialog();

        }

        void frm_Deactived(object entity)
        {
            //throw new NotImplementedException();
            this.source.Remove(entity as SupplyInfo);
        }


        void frm_Saved(object entity)
        {
            if (source == null)
                source = new BindingList<SupplyInfo>();
            var role = entity as SupplyInfo;
            var findresult = source.FirstOrDefault(a => a.supply_id == role.supply_id);
            if (findresult == null)
                this.source.Add(role);
        }
        void frm_SearchCompleted(string where)
        {
            //throw new NotImplementedException();

            this.LoadData(where);
        }

        void grd_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        void grd_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.RowIndex < grd.Rows.Count)
            {
                if (grd.Columns[e.ColumnIndex].Name == "supply_checkstatus")
                {
                    var currentstatus = grd.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); if (currentstatus == CheckStatus.Qualitified.ToString())
                    {
                        this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(84)))), ((int)(((byte)(175)))), ((int)(((byte)(14)))));
                    }
                    else if (currentstatus == CheckStatus.Rejected.ToString())
                    {
                        this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(230)))), ((int)(((byte)(94)))), ((int)(((byte)(32)))));
                    }
                    else if (currentstatus == CheckStatus.UnFinished.ToString())
                    {
                        this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(171)))), ((int)(((byte)(220)))));
                    }
                    else
                    {
    
                    }


                }
            }
        }
        public BindingList<SupplyInfo> source { get; set; }
        void ucStorage_Load(object sender, EventArgs e)
        {
            resource_id_column.DataSource = ResourcesHelper.GetAllResources();
            resource_id_column.DisplayMember = "ResourceName";
            resource_id_column.ValueMember = "ResourceId";


            supply_create_by.DataSource = UserInfoHelper.GetUsers();
            supply_create_by.DisplayMember = "UserName";
            supply_create_by.ValueMember = "UserId";

            //supply_update_by.DataSource = UserInfoHelper.GetUsers();
            //supply_update_by.DisplayMember = "UserName";
            //supply_update_by.ValueMember = "UserId";




            //if (!CheckPermission(1))
            //    this.btnAdd.Enabled = false;

            //if (!CheckPermission(2))
            //    this.btnEdit.Enabled = false;

            this.LoadData("1=1");
        }


        private void LoadData(string where)
        {
            this.source = new BindingList<SupplyInfo>(SupplyInfoHelper.GetAllSupplyInfo(where));
            this.grd.DataSource = new BindingSource(this.source, null);
        }




    }
}
